<?php
/**
 * Created by PhpStorm.
 * User: PanChaoZhi
 * Date: 2017/1/21
 * Time: 17:47
 */

/**
 * 使用单例模式来试写一个连接数据库类
 */
class Database
{
    private static $db;

    function __construct()
    {

    }

    /**
     * 实例化这个类
     * @return object Database
     */
    static function getInstance()
    {
        if (self::$db) {
            return self::$db;
        } else {
            return self::$db = new self();
        }
    }
}

class MySQL
{
    private static $db;
    private $host = 'localhost';
    private $username = 'root';
    private $password = 'localhost';
    private $dbname = 'shopimooc';
    private $dbcharset = 'utf-8';
    private $error = '数据库连接错误';

    /**
     * 连接数据库，用单例模式怎么写？结合工厂模式呢？
     * @return mysqli
     */
    public function connect()
    {
        self::$db = new mysqli($this->host, $this->username, $this->password, $this->dbname) or die($this->error . "," . mysqli_errno(self::connect()) . ":" . mysqli_error(self::connect()));
        self::$db->set_charset($this->dbcharset);
        return self::$db;
    }

    /**
     * 获取结果集的一条记录
     * @param $sql
     * @param int $result_type
     * @return array|null
     */
    public function fetchOne($sql, $result_type = MYSQLI_ASSOC)
    {
        $result = mysqli_query(self::$db, $sql);
        $row = mysqli_fetch_array($result, $result_type);
        return $row;
    }

    /**
     * 获取所有的结果集
     * @param $sql
     * @param int $result_type
     * @return array
     */
    public function fetchAll($sql, $result_type = MYSQLI_ASSOC)
    {
        $result = mysqli_query(self::$db, $sql);
        $rows = array();
        while (@$row = mysqli_fetch_array($result, $result_type)) {
            $rows[] = $row;
        }
        return $rows;
    }

    /**
     * 得到结果集的记录条数
     * @param $sql
     * @return int
     */
    public function getResultNum($sql)
    {
        $result = mysqli_query(self::$db, $sql);
        return mysqli_num_rows($result);
    }

    /**
     * 得到上一步插入记录的 ID
     * @return int|string
     */
    public function getInsertId()
    {
        return mysqli_insert_id(self::$db);
    }

    /**
     * 得到所有的分类
     * @return array
     */
    public function getAllCate()
    {
        $sql = "select * from imooc_cate";
        $rows = self::fetchAll($sql);
        return $rows;
    }

    /**
     * 提示信息的操作
     * @param $mes
     * @param $url
     */
    public function alertMes($mes, $url)
    {
        echo "<script>alert('{$mes}');</script>";
        echo "<script>window.location='{$url}';</script>";
    }

    /**
     * 根据 cid 得到4条记录
     * @param $cid
     * @return array
     */
    public function getProsByCid($cid)
    {
        $sql = "select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.cId={$cid} limit 4";
        $rows = self::fetchAll($sql);
        return $rows;
    }

    /**
     * 根据商品id得到商品图片
     * @param $id
     * @return array|null
     */
    public function getProImgById($id)
    {
        $sql = "select albumPath from imooc_album where pid={$id} limit 1";
        $row = self::fetchOne($sql);
        return $row;
    }

    /**
     * 得到4条产品
     * @param $cid
     * @return array
     */
    public function getSmallProsByCid($cid)
    {
        $sql = "select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.cId={$cid} limit 4,4";
        $rows = self::fetchAll($sql);
        return $rows;
    }

    /**
     * 根据id得到商品的详细信息
     * @param $id
     * @return array|null
     */
    public function getProById($id)
    {
        $sql = "select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.id={$id}";
        $row = self::fetchOne($sql);
        return $row;
    }

    /**
     * 根据商品id得到所有图片
     * @param $id
     * @return array
     */
    public function getProImgsById($id)
    {
        $sql = "select albumPath from imooc_album where pid={$id} ";
        $rows = self::fetchAll($sql);
        return $rows;
    }

    public function login()
    {
        session_start();
        $username = $_POST['username'];
        // addslashes():使用反斜线引用特殊字符
        // $username=addslashes($username);
        $username = mysqli_escape_string(self::connect(), $username);
        $password = md5($_POST['password']);
        $sql = "select * from imooc_user where username='{$username}' and password='{$password}'";
        // $resNum=getResultNum($sql);
        $row = self::fetchOne($sql);
        // echo $resNum;
        if ($row) {
            $_SESSION['login_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];
            $mes = "登陆成功！<br/>3秒钟后跳转到首页<meta http-equiv='refresh' content='3;url=index.php'/>";
        } else {
            $mes = "登陆失败！<a href='../login.php'>重新登陆</a>";
        }
        return $mes;
    }
}